<template>
  <el-dialog :title="basicInputDeals.editStatue=='edit'?'编辑输入':'添加输入'"
  :before-close="cancel"
             :visible.sync="basicInputDeals.showDialog" align="center">
    <el-form :model="editItem" :rules="rules2" ref="editItem" label-width="7em"
             style="height:500px;overflow-y: auto;padding-right:10px;">
      <el-form-item label="文件名" prop="key" style="margin-bottom: 22px;">
        <el-input v-model="editItem.key" :disabled="editItem.model" maxlength="50"></el-input>
      </el-form-item>
<!--      <el-form-item label="输入类别" align="left" style="margin-bottom: 22px;">-->
<!--        <el-select v-model="editItem.cat" style="width: 100%">-->
<!--          <el-option-->
<!--            v-for="item in options"-->
<!--            :key="item.value"-->
<!--            :label="item.label"-->
<!--            :value="item.value">-->
<!--          </el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item label="字段配置"  align="left" style="margin-bottom: 22px;">
        <el-row >
          <!--          <el-col :span="22">-->
          <!--            <el-radio-group v-model="editItem.access">-->
          <!--              <el-radio label="SAME">与输入一致</el-radio>-->
          <!--              <el-radio label="APPEND">在输出集追加</el-radio>-->
          <!--              <el-radio label="UNKNOWN">未知</el-radio>-->
          <!--            </el-radio-group>-->
          <!--          </el-col>-->
          <template>
            <el-col :span="6" style="background-color: rgba(0,0,0,0.05);padding-left: 10px">
              字段名
            </el-col>
            <el-col :span="4" style="background-color: rgba(0,0,0,0.05)">
              类型
            </el-col>
            <el-col :span="4" style="background-color: rgba(0,0,0,0.05)">
              序号
            </el-col>
            <el-col :span="4" style="background-color: rgba(0,0,0,0.05)">
              单位
            </el-col>
            <el-col :span="4" style="background-color: rgba(0,0,0,0.05)">
              描述
            </el-col>
            <el-col :span="2" style="background-color: rgba(0,0,0,0.05)" align="center">
              <i class="fa fa-plus-square-o" title="添加字段" @click="addFields.push({name:'',type:'String',index:'',measure:'',describe:''})" style="cursor: pointer"></i>
            </el-col>

            <template v-for="(item,index) in addFields" >
              <el-col :span="6" style="background-color: rgba(0,0,0,0.05);padding-left: 10px">
                <el-input v-model="item.name" placeholder="请输入字段名称" size="small" style="width: 110px;"></el-input>
              </el-col>

              <el-col :span="4" style="background-color: rgba(0,0,0,0.05)">
                <el-select v-model="item.type" size="small" style="width: 78px;">
                  <el-option
                    v-for="item in dataType"
                    :label="item.label"
                    :key="item.value"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-col>

              <el-col :span="4" style="background-color: rgba(0,0,0,0.05);">
                <el-input v-model="item.index" placeholder="请输入序号" size="small" style="width: 80px;"></el-input>
              </el-col>

              <el-col :span="4" style="background-color: rgba(0,0,0,0.05);">
                <el-input v-model="item.measure" placeholder="请输入单位" size="small" style="width: 79px;"></el-input>
              </el-col>

              <el-col :span="4" style="background-color: rgba(0,0,0,0.05);">
                <el-input v-model="item.describe" placeholder="请输入描述" size="small" style="width: 80px;"></el-input>
              </el-col>

              <el-col :span="2" style="background-color: rgba(0,0,0,0.05)" align="center">
                <i class="fa fa-trash-o" @click="addFields.splice(index,1)"></i>
              </el-col>

            </template>
          </template>
        </el-row>
      </el-form-item>

      <el-form-item label="描述" prop="description" style="margin-bottom: 22px;">
        <el-input type="textarea" placeholder="输入数据的描述" v-model.trim="editItem.description"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm('editItem')">确 认</el-button>
      <el-button @click="cancel">取 消</el-button>
    </span>
  </el-dialog>
</template>
<style>
</style>
<script>
export default {
  name: "EditDialog",
  data: function() {
    var validateKey = (rule, value, callback) => {
      if (value.endsWith(".txt") || value.endsWith(".dat")) {
        callback();
      } else {
        callback(new Error("请输入文件名以 .txt 或者 .dat 结尾"));
      }
      // if (value === "") {
      //   callback(new Error("请输入名称"));
      // } else {
      //   if (this.editItem.key !== "") {
      //     var reg = /^\w.+$/; //数字、26个英文字母或者下划线组成的字符串
      //     if (!reg.test(value)) {
      //       callback(
      //         new Error("请使用数字、26个英文字母或者下划线组成的字符串")
      //       );
      //     }
      //     callback();
      //   }
      //   callback();
      // }
    };
    return {
      rules2: {
        key: [
          { required: true, validator: validateKey, trigger: "blur" },
          { min: 1, max: 50, message: "长度在 1 到 50 个字符", trigger: "blur" }
        ],
        description: [
          { required: true, message: "请输入描述信息", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "长度在 1 到 300 个字符",
            trigger: "blur"
          }
        ]
      },
      options: [
        { value: "DATA", label: "数据" },
        { value: "MODEL", label: "模型" },
        { value: "UNSTRUCT", label: "非结构化文件" }
      ],
      dataType: [
        { label: "String", value: "String" },
        { label: "Double", value: "Double" },
        { label: "Int", value: "Int" },
        { label: "Bool", value: "Bool" }
      ],
      addFields: []
    };
  },
  computed: {
    basicInputDeals: function() {
      //弹出框状态
      return this.$store.state.systemComponent.componentsDeals.basicInputDeals;
    },
    editItem: function() {
      //当前编辑项
      // console.log(JSON.parse(this.basicInputDeals.editItem.columns || ''));
      // console.log('editItem>>>>>>>',JSON.parse(JSON.stringify(this.basicInputDeals.editItem)));

      if (this.basicInputDeals.editItem.columns) {
        let list = JSON.parse(
          JSON.stringify(this.basicInputDeals.editItem.columns)
        );
        // console.log(list);
        this.addFields = JSON.parse(list); //回显配置的字段
      }

      return this.basicInputDeals.editItem;
    },
    editComponent: function() {
      return this.$store.state.systemComponent.componentsDeals.editComponent;
    }
  },
  methods: {
    cancel() {
      this.basicInputDeals.showDialog = false;
      this.addFields = [];
      this.$refs.editItem.clearValidate();
    },
    submitForm(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.editSave();
        } else {
          return false;
        }
      });
    },
    editSave: function() {
      //确定
      try {
        if (this.basicInputDeals.editStatue == "add") {
          //新增时
          // if (this.editComponent.inputs.length < this.editComponent.minimumInput) {
          //   var newItem = new Object();
          //   newItem.key = this.editItem.key;
          //   newItem.cat = this.editItem.cat;
          //   newItem.description = this.editItem.description;
          //   newItem.columns = JSON.stringify(this.addFields);
          //
          //   this.editComponent.inputs.push(newItem);//添加
          // } else {
          //   this.$notify({
          //     type: 'warning',
          //     message: '添加输入项超出设定值，请在【基础信息】选项修改后重试!'
          //   });
          // }
          var newItem = new Object();
          newItem.key = this.editItem.key;
          newItem.cat = this.editItem.cat || "DATA";
          newItem.description = this.editItem.description;

          newItem.access = "APPEND";
          newItem.canPreview = false;

          // console.log(this.addFields);
          newItem["columns"] = JSON.stringify(this.addFields);

          this.editComponent.inputs.push(newItem); //添加

          // console.log(this.editComponent);
        } else if (this.basicInputDeals.editStatue == "edit") {
          //修改时
          var that = this;
          var inputItem = this.editComponent.inputs.filter(
            node => node.id == that.editItem.id
          )[0];
          inputItem.key = this.editItem.key;
          inputItem.cat = this.editItem.cat || "DATA";

          inputItem.access = "APPEND";
          inputItem.canPreview = false;

          inputItem.description = this.editItem.description;
          inputItem.columns = JSON.stringify(this.addFields);
        }
        this.$refs.editItem.resetFields();
      } catch (e) {
        console.error(e.message);
      }
      this.basicInputDeals.showDialog = false;
    },
    modelChange: function(val) {
      if (val) {
        this.editItem.key = "model";
      }
    }
  }
};
</script>
